package com.iretailer.report.quartz.sendReqularly;

import com.iretailer.report.util.TimeUtil;
import org.quartz.CronExpression;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther Bennie
 * @Date 2020/6/15 10:38
 * @Description
 */

public class CronTypeUtil {
    private static final Logger log = LoggerFactory.getLogger(CronTypeUtil.class);

    private CronTypeUtil() {
    }

    public enum CrontType {
        M_5("5m"),
        M_10("10m"),
        M_15("15m"),
        M_30("30m"),
        H_1("1h"),
        D_1("1d");
        private String code;

        CrontType(String code) {
            this.code = code;
        }

        private String getCode() {
            return this.code;
        }
    }

    private static class CronTypeMap {
        private static Map<String, CronExpression> map = new HashMap<>();

        private static Map<String, CronExpression> getCronExpressionMap() throws ParseException {
            map.put("5m", new CronExpression("0 */5 * * * ?"));
            map.put("10m", new CronExpression("0 */10 * * * ?"));
            map.put("15m", new CronExpression("0 */15 * * * ?"));
            map.put("30m", new CronExpression("0 */30 * * * ?"));
            map.put("1h", new CronExpression("0 0 */1 * * ?"));
            map.put("60m", new CronExpression("0 0 */1 * * ?"));
            map.put("1d", new CronExpression("0 5 1 */1 * ?"));
            return map;
        }
    }

    public static Map<String, CronExpression> getCronExpressionMap() {
        try {
            return CronTypeMap.getCronExpressionMap();
        } catch (ParseException e) {
            log.error(e.getMessage(), e);
            return Collections.emptyMap();
        }
    }


    public static Map<String, String> getStartAndEndTime(ZonedDateTime zonedDateTime, String type) {
        Map<String, String> result = new HashMap<>();
        if (type.indexOf("m") > 0) {
            int period = Integer.parseInt(type.replace("m", ""));
            ZonedDateTime end = zonedDateTime.plusMinutes(-period);
            ZonedDateTime start = end.plusMinutes(-period);
            result.put("StartTime", TimeUtil.zoneDateTime_format(start));
            result.put("EndTime", TimeUtil.zoneDateTime_format(end));
        } else if (type.indexOf("h") > 0) {
            int period = Integer.parseInt(type.replace("h", ""));
            ZonedDateTime end = zonedDateTime.plusHours(-period);
            ZonedDateTime start = end.plusHours(-period);
            result.put("StartTime", TimeUtil.zoneDateTime_format(start));
            result.put("EndTime", TimeUtil.zoneDateTime_format(end));
        } else if (type.indexOf("d") > 0) {
            int period = Integer.parseInt(type.replace("d", ""));
            String date = TimeUtil.localDate_format(zonedDateTime.toLocalDate().plusDays(-period));
            String end = TimeUtil.getEndTime(date);
            String start = TimeUtil.getStartTime(date);
            result.put("StartTime", start);
            result.put("EndTime", end);
        }
        return result;
    }

    public static String getEndTime(String dateTime, String type) {
        LocalDateTime t = TimeUtil.parse_LocalDateTime(dateTime);
        if (type.indexOf("m") > 0) {
            int period = Integer.parseInt(type.replace("m", ""));
            LocalDateTime end = t.plusMinutes(period);
            return TimeUtil.localDateTime_format(end);
        } else if (type.indexOf("h") > 0) {
            int period = Integer.parseInt(type.replace("h", ""));
            LocalDateTime end = t.plusMinutes(period);
            return TimeUtil.localDateTime_format(end);
        } else if (type.indexOf("d") > 0) {
            int period = Integer.parseInt(type.replace("d", ""));
            String date = TimeUtil.localDate_format(t.toLocalDate().plusDays(period));
            return TimeUtil.getEndTime(date);
        }
        return null;
    }


}
